package controlSoftware.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import controlSoftware.DataProcess.Device;

public class CustomListRenderer extends JLabel implements ListCellRenderer {

	/**
	 * Generated UID
	 */
	private static final long serialVersionUID = -8947385056470075351L;
	
	private JPanel cell;
	private JLabel icon;
	private JPanel description;
	private JTextArea deviceInfo;
    private JTextArea userInfo;


	public CustomListRenderer() {
        // sets up cell format
        cell = new JPanel(new BorderLayout());

        // icon
        //icon = new JLabel();
        //icon.setBounds(0,0,30,30);
        

        // description
        description = new JPanel(new BorderLayout());
          
        deviceInfo = new JTextArea();
        deviceInfo.setLineWrap(true);
        deviceInfo.setWrapStyleWord(true);
        Font titleFont = new Font("Helvetica", Font.BOLD, 12);
        deviceInfo.setFont(titleFont);
        
        userInfo = new JTextArea();
        userInfo.setLineWrap(true);
        userInfo.setWrapStyleWord(true);  
        Font descFont = new Font("Helvetica", Font.PLAIN, 12);
        userInfo.setFont(descFont);
        
        description.add(deviceInfo, BorderLayout.NORTH);
        description.add(userInfo, BorderLayout.CENTER);
        
        cell.add(description, BorderLayout.CENTER);

    }

	@Override
	public Component getListCellRendererComponent(
							            JList list,
							            Object value,
							            int index,
							            boolean isSelected,
							            boolean cellHasFocus) {
		 
		deviceInfo.setText("Device " + ((Device)value).getId()) ;
		
		// check if an user has been assigned for the device
		if (((Device)value).getUser().getsMappedDevice() >= 0)
		{
			userInfo.setText(((Device)value).getUser().getsFirstName() + ", " + 
				((Device)value).getUser().getsLastName());
		}
	
		// Set the image icon
		icon = new JLabel(((Device)value).getUser().getIcon());
		//icon = new JLabel();
		//icon.setBounds(0,0,30,30);
		cell.add(icon, BorderLayout.WEST);
		//icon.setBounds(0,0,30,30);
        //l = new JLabel(((Device)value).getUser().getIcon()); // <-- this will be an icon instead of a
        
        //setBackground(isSelected ? Color.red : Color.white);
        //setForeground(isSelected ? Color.white : Color.black);


		return cell;

	}

}
